package com.jic.order.controller.manage;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.RestResult;
import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.order.entity.OrderSplit;
import com.jic.order.entity.OrderSplitCondition;
import com.jic.order.manage.request.OrderSplitEditRequest;
import com.jic.order.manage.request.OrderSplitRequest;
import com.jic.order.manage.response.OrderSplitResponse;
import com.jic.common.base.vo.PageResult;
import com.jic.order.service.OrderSplitConditionService;
import com.jic.order.service.OrderSplitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "订单拆分规则", tags = {"订单拆分规则"})
@RequestMapping(value = "orderSplit")
@RestController
public class OrderSplitController {

	@Autowired
	OrderSplitService orderSplitService;
	@Autowired
	OrderSplitConditionService orderSplitConditionService;

	@ApiOperation(value = "获取所有订单拆分信息")
	@ResponseBody
	@PostMapping(value = "/queryPage")
	public RestResult<PageResult<OrderSplitResponse>> queryPage(@RequestBody OrderSplitRequest form) {
		log.info("开始调用订单拆分分页，{}", JSON.toJSONString(form));
		Page page = new Page();
		page.setLimit(form.getLimit());
		page.setStart(form.getStart());
		OrderSplit orderSplit = new OrderSplit();
		BeanUtils.copyProperties(form, orderSplit);
		DeptTypeEnums deptType = form.getAdminOperate().getDeptType();
		if (deptType.getCode() != 0) {
			orderSplit.setDeptId(form.getDeptId());
		}
		PageResult<OrderSplit> result = orderSplitService.queryPage(orderSplit, page);
		List<OrderSplitResponse> orderSplitResponseList = result.getRows().stream().map(temp -> {
			OrderSplitResponse orderSplitResponse = new OrderSplitResponse();
			BeanUtils.copyProperties(temp, orderSplitResponse);
			return orderSplitResponse;
		}).filter(item -> item != null).collect(Collectors.toList());
		PageResult<OrderSplitResponse> pageResult = new PageResult<>();
		pageResult.setRows(orderSplitResponseList);
		pageResult.setTotal(result.getTotal());
		return RestResult.success(pageResult);

	}

	@ApiOperation(value = "订单拆分规则新增编辑")
	@ResponseBody
	@PostMapping(value = "/editOrderSplit")
	public RestResult editOrderSplit(@RequestBody OrderSplitEditRequest orderSplitEditRequest) {
		log.info("开始调用订单拆分新增编辑，{}", JSON.toJSONString(orderSplitEditRequest));
		RestResult restResult = new RestResult<>();
		try {
			OrderSplit orderSplit = new OrderSplit();
			BeanUtils.copyProperties(orderSplitEditRequest, orderSplit);
			orderSplit.setDeptId(orderSplitEditRequest.getAdminOperate().getDeptId());
			orderSplitService.editOrderSplit(orderSplit);
			return RestResult.success(true);
		} catch (Exception e) {
			log.error("editOrderSplit========》", e);
		}
		return restResult;

	}

	@ApiOperation(value = "批量停用订单拆分")
	@ResponseBody
	@PostMapping(value = "/editOrderSplitStatus")
	public RestResult editOrderSplitStatus(@RequestBody OrderSplitEditRequest orderSplitEditRequest) {
		log.info("开始调用批量停用订单拆分，{}", orderSplitEditRequest.toString());
		RestResult restResult = new RestResult<>();
		try {
			orderSplitService.editOrderSplitStatus(orderSplitEditRequest.getIds());
			return RestResult.success(true);
		} catch (Exception e) {
			log.error("editOrderSplitStatus========》", e);
		}
		return restResult;

	}

	@ApiOperation(value = "删除订单拆分规则")
	@ResponseBody
	@PostMapping(value = "/deleteOrderSplit")
	public RestResult deleteOrderSplit(@RequestBody OrderSplitEditRequest orderSplitEditRequest) {
		log.info("开始调用删除订单拆分规则，{}", orderSplitEditRequest.toString());
		RestResult restResult = new RestResult<>();
		try {
			orderSplitService.delete(orderSplitEditRequest.getIds());
			return RestResult.success(true);
		} catch (Exception e) {
			log.error("deleteOrderSplit========》", e);
		}
		return restResult;

	}

	@ApiOperation(value = "删除订单拆分规则")
	@ResponseBody
	@PostMapping(value = "/queryOrderSplitCondition")
	public RestResult queryOrderSplitCondition(@RequestBody OrderSplitRequest orderSplitRequest) {
		log.info("开始调用订单拆分规则匹配条件，{}", orderSplitRequest);
		OrderSplitCondition orderSplitCondition = new OrderSplitCondition();
		orderSplitCondition.setOrderSplitId(orderSplitRequest.getId());
		RestResult restResult = new RestResult<>();
		try {
			List<OrderSplitCondition> orderSplitConditions = orderSplitConditionService.query(orderSplitCondition);
			return RestResult.success(orderSplitConditions);
		} catch (Exception e) {
			log.error("queryOrderSplitCondition========》", e);
		}
		return restResult;

	}
}
